package com.hui.mapper;


import com.hui.dto.MenuPageDTO;
import com.hui.entity.Menu;
import com.hui.entity.RoleMenu;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MenuMapper {

    @Insert("""
            insert into ums_menu (title, url, icon, pid, info, version, deleted, created, updated)
            values (#{title}, #{url}, #{icon}, #{pid}, #{info}, #{version}, #{deleted}, #{created}, #{updated})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Menu menu);

    @Results(id = "menuResultMap", value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "pid", property = "pid"),
            @Result(column = "pid", property = "parent",
                    one = @One(select = "com.hui.mapper.MenuMapper.select")),
    })
    @Select("""
            select * from ums_menu t where t.id = #{param1} and t.deleted = 0
            """)
    Menu select(Long id);

    @ResultMap("menuResultMap")
    @Select("""
            <script>
            select * from ums_menu t
            <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                <if test='pid != null'> pid = #{pid} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Menu> list(MenuPageDTO dto);

    @Update("""
            <script>
            update ums_menu
            <set>
            <if test='title != null'> title = #{title}, </if>
            <if test='url != null'> url = #{url}, </if>
            <if test='icon != null'> icon = #{icon}, </if>
            <if test='pid != null'> pid = #{pid}, </if>
            <if test='info != null'> info = #{info}, </if>
            <if test='deleted != null'> deleted = #{deleted}, </if>
            <if test='created != null'> created = #{created}, </if>
            <if test='updated != null'> updated = #{updated}, </if>
            version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(Menu menu);

    @Update("""
            update ums_menu set deleted = 1, updated = current_timestamp where id = #{param1}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_menu set deleted = 1, updated = current_timestamp
            where id in
            <foreach collection='list' item='e' open='(' close=')' separator=','>
                ${e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Select("""
		select * from ums_menu t2 where id in (
			select fk_menu_id from ums_role_menu t1
			where t1.fk_role_id = #{param1} and t1.deleted = 0
		) and t2.deleted = 0
		""")
    List<Menu> listByRoleId(Long roleId);


    @Update("""
		update ums_role_menu set deleted = 1, updated = now() where fk_role_id = #{param1}
		""")
    int deleteRoleMenuByRoleId(Long roleId);

    @Insert("""
        <script>
        insert into ums_role_menu (fk_role_id, fk_menu_id, version, deleted, created, updated)
        values
        <foreach collection='list' item='e' separator=','>
            (#{e.fkRoleId}, #{e.fkMenuId}, #{e.version}, #{e.deleted}, #{e.created}, #{e.updated})
        </foreach>
        </script>
        """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertRoleMenuBatch(List<RoleMenu> roleMenus);


    @Select("""
		select * from ums_menu t1 where id in(
			select t2.fk_menu_id from ums_role_menu t2 where t2.fk_role_id in (
				select t1.fk_role_id from ums_emp_role t1
				where t1.fk_emp_id = #{param1} and t1.deleted = 0
			) and t2.deleted = 0
		) and t1.deleted = 0
		""")
    List<Menu> listByEmpId(Long empId);

}
